﻿/*
把xml数据转换为table展现
_xml: 需要展现的xml数据
_tableID: 需要填充数据的table
_rowTemplate: 行模板
_rowClass: 行style class name
_rowAltClass: 行alt class name

Cola 2010-8-24
*/
function XmlToTableParse(_xml, _tableID, _rowTemplate, _rowClass, _outputTargetObj, _rowDeletedEvent, _rowsAddedEvent) {
    var Obj = this;
    this.XML = _xml;
    this.TableID = _tableID;
    this.RowTemplate = _rowTemplate;
    this.RowClass = _rowClass;
    this.OutputHTML = "";

    this.render = function (_xpath) {
        
        $(this.XML).find(_xpath).each(function () {
            var row = Obj.RowTemplate;

            $(this).children().each(function () {
                var str = new RegExp("{" + $(this)[0].nodeName + "}", "gi");

                row = row.replace(str, $(this)[0].text);

            });

            //debugger;
            //alert(_outputTargetObj.value);
            //alert($(this).children(0)[0].text);
            if (_outputTargetObj.value.indexOf($(this).children(0)[0].text) != 0) {

                $('#' + Obj.TableID + " tbody").append(row);

                var lastRow = $('#' + Obj.TableID + " tbody tr:last");
                //
                //alert(lastRow.find("td:first").text);
                _outputTargetObj.value += (lastRow.find("td:first")[0].innerText + ";");
                //alert(_outputTargetObj.value);
                //添加删除按钮
                //
                lastRow.append("<td><img class='img_click' src='../../../resources/images/icon_delete.gif' /></td>");

                //添加删除按钮事件
                lastRow.find('.img_click').click(function () {
                    _outputTargetObj.value = _outputTargetObj.value.replace(lastRow.find("td:first")[0].innerText + ";", "");
                    lastRow.remove();
                    _rowDeletedEvent();
                });
            }
        });
        _rowsAddedEvent();
    }
}